<template>
  <div class="container-fluid">
    <div class="row school-model school-tiao">
      <div class="col school-tiao-left">
        <span>{{ $route.name }}</span>
      </div>
      <div class="col text-right">
        <a-button-group>
          <a-button type="primary" @click="add()">添加题库组</a-button>
        </a-button-group>
      </div>
    </div>
    <div class="row school-model index-model">
      <div class="col school-col-table">
        <el-table
          :data="tableData.slice((currentPage - 1) * pagesize, currentPage * pagesize)"
          style="width: 100%; border: 1px solid #eeeeee"
        >
          <el-table-column prop="ID" label="序号" width="60">
            <template slot-scope="scope">{{scope.$index+1}}</template>
          </el-table-column>
          <el-table-column prop="name" label="题库组名称"></el-table-column>
          <el-table-column align="right" width="300px">
            <template slot-scope="scope">
              <el-button size="mini" type="primary">
                <router-link class="text-white" :to="{name:'题库详情', params: { ID: scope.row.ID }}">查看</router-link>
              </el-button>
              <el-button type="primary" size="mini" @click="add(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="del(scope.row.ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          small
          @current-change="currentChange"
          :page-size="pagesize"
          layout="total, prev, pager, next"
          :total="tableData.length"
        ></el-pagination>

        <el-dialog class="school-form" :title="title" width="510px" :visible.sync="setaddress">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" size="mini">
            <el-form-item label="题库组名称:" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
            <el-button @click="setaddress = false" size="mini">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  asynccreateschoolitembankgroup,
  asyncdelschoolitembankgroup
} from "../../../../plugins/api.js";
import { totime } from "../../../utils.js";
export default {
  name: "schoolitembankgroup",
  data() {
    return {
      index: 0,
      isaddress: true,
      setaddress: false,
      search: "",
      currentPage: 1,
      pagesize: 10,
      title: "添加题库组",
      ruleForm: {
        ID: "",
        name: ""
      },
      rules: {
        name: [
          { required: true, message: "请输入题库组名称", trigger: "blur" },
          { min: 1, max: 30, message: "长度在 1 到 30 个字符", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["getschoolitembankgroup"]),
    tableData() {
      const { getschoolitembankgroup, search } = this;
      return getschoolitembankgroup.filter(data => {
        return search ? data.name.includes(search) : true;
      });
    }
  },
  methods: {
    totime,
    currentChange(index) {
      this.currentPage = index;
    },
    add(v) {
      this.setaddress = true;
      if (v) {
        this.title = "修改题库组";
        this.ruleForm = JSON.parse(JSON.stringify(v));
      } else {
        this.title = "添加题库组";
        this.ruleForm = {
          name: ""
        };
      }
    },
    del(ID) {
      this.$confirm("此操作将永久删除该题库组, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            await asyncdelschoolitembankgroup({ ID });
            await this.$store.dispatch("actionschoolitembankgroup");
            this.$message.success("删除成功");
          } catch (err) {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async submitForm() {
      this.$refs["ruleForm"].validate(async valid => {
        if (valid) {
          if (this.$store.state.state) return;
          this.$store.commit("setstate", true);
          const data = this.ruleForm.ID ? "修改" : " 添加";
          try {
            await asynccreateschoolitembankgroup(this.ruleForm);
            await this.$store.dispatch("actionschoolitembankgroup");
            this.setaddress = false;
            this.$message.success(`${data}成功`);
          } catch (err) {
            this.$message.error(`${data}失败`);
          }
          this.$store.commit("setstate", false);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.index-model {
  margin-top: 13px;
}
.dialog-footer {
  text-align: center;
}
</style>